import cv2
import numpy as np
import json

"""
使用由labelme标注好的json文件和原图生成灰度图数据集，背景像素值为0，类别的像素值依次为1、2、3...
"""
category_types = ["Background", "LCX", "LM", "LAD"]

img = cv2.imread(r"D:\Files\deepLearningData\lcx_lad1\IM000000_0006.jpg")
h, w = img.shape[:2]
mask = np.zeros([h, w, 1], np.uint8)

with open(r"D:\Files\deepLearningData\lcx_lad1\IM000000_0006.json", "r") as f:
    label = json.load(f)

shapes = label["shapes"]
for shape in shapes:
    category = shape["label"]
    points = shape["points"]
    points_array = np.array(points, dtype=np.int32)
    mask = cv2.fillPoly(mask, [points_array], category_types.index(category))

cv2.imwrite(r"D:\Files\deepLearningData\lcx_lad1\annotations\IM000000_0006.png", mask)
